package com.vteApp;

import java.util.Iterator;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vteApp.bl.MockRickDB;
import com.vteApp.bl.Risk;

public class ScoreActivity extends VTEAppActivity implements OnClickListener {

	TextView tv;

	@Override
	protected void onStart() {
		super.onStart();
		setContentView(R.layout.scores);
		findViewById(R.id.save_risks).setOnClickListener(this);
		findViewById(R.id.button_email).setOnClickListener(this);
		findViewById(R.id.button_back).setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		int score = evaluateScore();
		String riskLevel;
		if (score < 2)
			riskLevel = getResources().getString(R.string.low);
		else if (score < 3)
			riskLevel = getResources().getString(R.string.mid);
		else if (score < 5)
			riskLevel = getResources().getString(R.string.high);
		else
			riskLevel = getResources().getString(R.string.highest);
		TextView tv = (TextView) findViewById(R.id.score_text_view_1);
		tv.setText(String.format(getResources().getString(R.string.risk_score),
				riskLevel, score));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save_risks:
			MockRickDB.getInstance(this).saveAllChanges();
			break;
		case R.id.button_email:
			sendEmail();
			break;
		case R.id.button_back:
			startActivity(new Intent().setClass(this, MainActivity.class));
			break;

		}

	}

	private int evaluateScore() {
		List<Risk> risks = MockRickDB.getInstance(this).getRisks();
		int score = 0;
		boolean[] scoreFlags = { false, false, false, false, false, false };
		for (Iterator<Risk> iterator = risks.iterator(); iterator.hasNext();) {
			Risk risk = (Risk) iterator.next();
			if (scoreFlags[risk.getRiskFactor() - 1] == false) {
				if (risk.getValue() == true) {
					score += risk.getRiskFactor();
					scoreFlags[risk.getRiskFactor() - 1] = true;
				}
			}
		}
		return score;
	}

	private void sendEmail() {
		// TODO Auto-generated method stub
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		emailIntent.setType("text/plain");
		startActivity(emailIntent);
		emailIntent
				.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
	}
}
